Recommend declare instead of disabling strictPropertyInitialization#145
Recommend declare instead of disabling strictPropertyInitialization#145keithamus merged 3 commits intogithub:mainfrom
declare instead of disabling strictPropertyInitialization#145Conversation
Since TypeScript 3.7, which was released in November 2019, TypeScript supports declaring properties which will be initialized in a way that is invisible to TS via the `declare` property modifier. This enables using `strictPropertyInitialization` with class or field decorators (as well as other patterns which can initialize classes invisibly to TS).
|
Yeah. The reason is a bit subtle; the forward compatibility here is not "will this thing be allowed in this position?" (to which the answer is "who knows but probably no" for both of them!), but rather "what are the semantics of the field created this way?" For
|
keithamus
left a comment
There was a problem hiding this comment.
Hey @chriskrycho thanks for this change! This is really well worded and fits within our documentation style. Kudos!
Sorry this took so long to merge. We're trying to find more ergonomic ways to handle this, but I think ultimately, for now, this accurately describes the solutions and so we should definitely make it visible.
|
All good – thanks for getting back around to it. (I have more than a few long-standing PRs to get back to on things I maintain myself. 😂) |
Since TypeScript 3.7, which was released in November 2019, TypeScript supports declaring properties which will be initialized in a way that is invisible to TS via the
declareproperty modifier. This enables usingstrictPropertyInitializationwith class or field decorators (as well as other patterns which can initialize classes invisibly to TS).Accordingly, this PR suggests switching to
declareand makes that the first recommended path.If you all prefer to keep the recommendation to switch off
strictPropertyInitialization(which means other properties can be accidentally uninitialized!), I'd strongly recommend usingdeclarein your workaround guidance instead of!, because!is not forward-compatible with the ES spec (see here for details).